package com.nitty.cash.loop.ui.presenter

import com.lib.common.permission.CommonPermissions
import com.lib.common.permission.Permission
import com.nitty.cash.loop.common.CommonPresenter
import com.nitty.cash.loop.configs.NittyApiPath
import com.nitty.cash.loop.configs.NittyApiUrl
import com.nitty.cash.loop.configs.NittyConfigs
import com.nitty.cash.loop.request.IHttpCallback
import com.nitty.cash.loop.ui.contract.IViewContract
import com.nitty.cash.loop.utils.helper.NittyAnalysisHelper
import com.nitty.cash.loop.utils.helper.NittyConfigsHelper
import com.nitty.cash.loop.utils.shared.NittySharedHelper
import com.nitty.cash.loop.utils.shared.NittySharedKey
import org.json.JSONObject
import java.util.TimeZone


/**
 * NittyCash
 * @Author wwdeng
 * @CreateTime: 2024/11/28 13:45
 * @Annotation：贷款申请 Presenter 实现
 */
class LaunchPresenter(private val iView: IViewContract.ILaunchView) : CommonPresenter(iView) {

    /**
     * 获取地区转发
     */
    fun getLaunchTimeZoneAction() {
        val params = JSONObject()
        params.put("p", NittyConfigs.NITTY_APPNAME)
        params.put("tz", TimeZone.getDefault().id)
        val httpUrl = NittyApiUrl.NITTY_HOST + NittyApiUrl.NITTY_TIMEZONE_URL
        doHttpPostAction(httpUrl, params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                iView.showLaunchTimeZoneRequestData(data.optString("data"))
            }
            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
            }
        })
    }

    /**
     * 获取APP配置信息
     */
    fun getLaunchConfigsAction() {
        val params = JSONObject().apply {
            put("w0148906f",NittyConfigs.NITTY_APPNAME)
            put("xd304965bcd9478080",NittyApiPath.NITTY_PATH.NITTY_API_APP_INFO_PATH)
        }
        val httpUrl = NittyApiUrl.getHttpUrl()
        doHttpPostAction(httpUrl, params,object : IHttpCallback{
            override fun onRequestSuccess(data: JSONObject) {
                iView.hiddenNittyLoading()
                val obj = data.optJSONObject("obj")
                //保存所有数据
                if (obj != null) {
                    NittySharedHelper.putNittySharedData(NittySharedKey.NITTY_COMMON,obj.toString())
                }
                showLaunchIntentPage()
            }
            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
                iView.hiddenNittyLoading()
            }
        })
    }

    private fun showLaunchIntentPage(){
        //是否显示引导页
        if (!NittySharedHelper.getNittySharedBoolean(NittySharedKey.NITTY_SHOW_GUIDE_PAGE)){
            iView.showLaunchGuidePage()
            return
        }

        //是否进入过权限页面 注：此字段只在同意 READ_PHONE_STATE 权限之后改为 true
        val isShowLaunchPermission = NittySharedHelper.getNittySharedBoolean(NittySharedKey.NITTY_SHOW_PERMISSION_LAUNCH_PAGE)
        if (!isShowLaunchPermission){
            //进入权限页
            iView.showLaunchPermissionPage()
            return
        }
        // TODO: PhoneState为必须申请的权限
        if (NittyConfigsHelper.getNittyPermissionsLaunch().contains(Permission.READ_PHONE_STATE) && !CommonPermissions.isGranted(iView.getAct(),Permission.READ_PHONE_STATE)){
            //进入权限页
            iView.showLaunchPermissionPage()
            return
        }

        //已登录 - 进入主页
        if (NittySharedHelper.getNittySharedBoolean(NittySharedKey.NITTY_LOGIN)){
            //初始化analysis lib 赋值 用户id 和 token
            NittyAnalysisHelper.initAnalysisLibAtLoginSuccess()
            iView.showLaunchMainPage()
            return
        }
        //进入登录页面
        iView.showLaunchLoginPage()
    }
}